7.9 summer vacation HW


Chapter 7


notes

1.for

for i in range(a,b)/list[a,b]
Every iteration of i , i equal to next variable
2.while <condition>:
Every integration of i, programmer must change the variable
3.while 1:
<statement>
if<condition>:
break
4.tracint a program
Being able to set a breakpoint where you need one is far more powerful. So we strongly encourage you to invest time in learning using to use your programming environment (PyScripter, in these notes) to full effect.
print('...')
5.count digits
def num_zero_and_five_digits(n):
    count = 0
    while n > 0:
        digit = n % 10
        if digit == 0 or digit == 5:
            count = count + 1
        n = n // 10
    return count


exercise

1.
n = 0
a =112,22,3,4,59,2for i in a:
    if i % 2 == 1:
        n = n + 1
print (n)
2.
a =1,3,4,2,5,6,7]
Sum = 0
for i in a :
    if i % 2 == 0:
        Sum = Sum + i
print (Sum)
 
3.
a = [-2,-4,25,-6,2]
Sum = 0
for i in a:
    if i < 0 :
        Sum = Sum + i
print (Sum)
 
4.
a ='asdfc','sdfi','sdfkj','d','asdas']
count = 0
for i in a:
    if len(i) == 5:
        count = count + 1
print(count)
 
5.
a =1,2,3,4,5,6,7,8,9]
Sum = 0
count = 0
for i in a :
    if i % 2 == 0:
        count = count + 1
        if count != 1:
            Sum = Sum + i
    else:
        Sum = Sum + i
print(Sum)
6.
a = ['aaa','bbb','ccc','sum','ccc']
n = 0
for i in a:
    if i == 'sum':
        n = n + 1
        break
    else:
        n = n + 1
print (n)
8.
def  print_mult_table(n):
    for i in range(1,n):
        for j in range(1,i+1):
            print(j,"*",i,"=",i*j,end=' ')
        print('   ')
print_mult_table(10)
 
9.
def print_triangular_number(n):
    a=0
    for i in range(1,n+1):
        a = a + i
        print (i,'     ',a)
 
print_triangular_number(5)
10.
def is_prime(n):
    if n < 2:
        return False
    else:
        for i in range(2,n):
            if n % i == 0:
                return True
            return False
print(is_prime(19911121))
11.
import turtle
a=[160,-43,270,-43]
b=[20,10,8,12]
n=0
for i in b:
    n+=1
    turtle.left(i)
    turtle.forward(a[n-1])
 
12.
import turtle
import math
 
data=[(-100,100),(0,100),(-100,0),(0,0),(0,100),(-50,100+50/math.sin(45)),(-100,100),(-100,0),(0,0)]
 
scr=turtle.Screen()
alex=turtle.Turtle()
alex.pensize(5)
alex.speed(0)
alex.left(90)
 
for (i,j) in data:
    alex.goto(i,j)
 
scr.mainloop()
13.
2、5
14.
def num_digits(n):
    digit = 0
    while 1:
        if -10 < n < 10:
            return digit+1
        n = n // 10
        digit=digit+1
print(num_digits(1000))
15.
def even_number_digit(n):
    digit = 0
    while not  -2 < n < 2:
        if n % 2 == 0 and n % 10 != 0:
            digit += 1
        n = n // 10
    return digit
print(even_number_digit(1230))
16
def sum_of_squares(xs):
    sum = 0
    for i in xs:
        sum = sum + i**2
    return sum
 
xs=[0]
print(sum_of_squares(xs))
17.
# Your friend will complete this function
def play_once(human_plays_first):
    """
       Must play one round of the game. If the parameter
       is True, the human gets to play first, else the
       computer gets to play first.  When the round ends,
       the return value of the function is one of
       -1 (human wins),  0 (game drawn),   1 (computer wins).
    """
    # This is all dummy scaffolding code right at the moment...
    import random                  # See Modules chapter ...
    rng = random.Random()
    # Pick a random result between -1 and 1.
    result = rng.randrange(-1,2)
    print("Human plays first={0},  winner={1} "
                       .format(human_plays_first, result))
    return result
def play_first(n):
    if n % 2 == 0:
        return True
        print('human first')
    else:
        return False
        print('computer first')
def main():
    human=0
    computer=0
    drawn=0
    while input('do you want to play again'=='yes'):
        result=play_once(play_first(n),human,drawn,computer)
        if result == 1:
            print ('you win !')
            human = human + 1
        elif result == -1:
            print('coumpter win !')
            computer = computer + 1
        else:
            print('game drawn!')
            drawn = drawn + 1
    else:
        print('your score is',human,'the win rate is',human/(human+computer+drawn))